package com.hr.runnerbuddy;

import com.hr.runnerbuddy.database.DBAdapter;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;

public class GpsList extends ListActivity {

    protected Cursor cursor=null;
    protected ListAdapter adapter;
    private DBAdapter databasehelper;
    private DBAdapter dbadap;
        
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.list);
        
        databasehelper = new DBAdapter(getApplicationContext());
       
//        SQLiteDatabase db = databasehelper.getWritableDatabase();
//        Cursor cursor = db.rawQuery("SELECT COUNT(*) FROM gpsdata", null);

        if (cursor.getCount() != 0)
        {
        	dbadap = new DBAdapter(this);
	        dbadap.open();
	        
//	        cursor = dbadap.fetchAllGpsData();
	        cursor.moveToFirst();
	        
//	        String[] from = new String[]{DBAdapter.KEY_LAT, DBAdapter.KEY_LON};
	        
//	        Toast.makeText(getApplicationContext(), Integer.toString(dbadap.fetchAllGpsData().getCount()), Toast.LENGTH_LONG).show();
	        
	        //int[] to = new int[] { R.id.latituderow, R.id.longituderow };
	        
//	        adapter = new SimpleCursorAdapter(this, R.layout.listrow, cursor, from, to);
	        setListAdapter(adapter);
	        
//	        Integer.toString(dbadap.fetchAllGpsData().getColumnCount());
//	        
	        dbadap.close();
        }

    }
    
    @SuppressWarnings("rawtypes")
   	public void navigateTo(Class c){
       	Intent intent = new Intent(GpsList.this, c);
       	startActivity(intent);
    }
       
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }
       
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
        case R.id.settings:
	        navigateTo(Settings.class);
	        return true;
        case R.id.main:
	        navigateTo(ParcourMap.class);
	        return true;
        default:
        	return super.onOptionsItemSelected(item);
        }
    }
    
    @Override
	protected void onDestroy() {
    	databasehelper.close();
		super.onDestroy();
	}
    
    public void onListItemClick(ListView parent, View view, int position, long id) {
    	/*
        Intent intent = new Intent(this, EmployeeDetails.class);
        Cursor cursor = (Cursor) adapter.getItem(position);
        intent.putExtra("EMPLOYEE_ID", cursor.getInt(cursor.getColumnIndex("_id")));
        startActivity(intent);
        */
    }
    
}